package com.qxzn.app.personalcenter.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.LoginAndRegisterModel;
import com.qxzn.app.page.widget.VerificationCodeView;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册2-验证码
 */
public class RegisterCodeActivity extends BaseActivity implements MyInterface.VerifyCodeListener, MyInterface.GetCodeListener {
    @BindView(R2.id.tv_register_title)
    TextView tvRegisterTitle;
    @BindView(R2.id.tv_phone_msg)
    TextView tvPhoneMsg;
    @BindView(R2.id.tv_resend)
    TextView tvResend;
    @BindView(R2.id.vcv_code)
    VerificationCodeView vcvCode;
    private LoginAndRegisterModel loginAndRegisterModel;
    private String phone;
    private CountDownTimer timer;
    private int type;

    @Override
    protected int initLayout() {
        return R.layout.activity_register_code;
    }

    @Override
    protected void initView() {
        phone = getIntent().getStringExtra("phone");
        type = getIntent().getIntExtra("type", 3);
        tvPhoneMsg.setText("短信验证码 至 " + phone);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loginAndRegisterModel = new LoginAndRegisterModel(this);
        loginAndRegisterModel.setVerifyCodeListener(this);
        loginAndRegisterModel.setGetCodeListener(this);

        vcvCode.setCompleteListener(new VerificationCodeView.InputCompleteListener() {
            @Override
            public void onInputComplete(String code) {
                JSONObject params = new JSONObject();
                params.put("username", phone);
                params.put("code", code);
                params.put("type", type);
                loginAndRegisterModel.verifyCode(params);
            }
        });

        countDownTimer();
    }

    private void countDownTimer() {
        if (timer == null) {
            timer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    tvResend.setText("重新发送(" + millisUntilFinished / 1000 + "s)");
                }

                @Override
                public void onFinish() {
                    tvResend.setEnabled(true);
                    tvResend.setText("重新发送");
                    tvResend.setTextColor(getResources().getColor(R.color.blue_2A8CFF));
                }
            };
        } else {
            timer.cancel();
        }
        timer.start();
        tvResend.setEnabled(false);
        tvResend.setTextColor(getResources().getColor(R.color.gray_FF999999));
    }

    @OnClick({R2.id.tv_resend})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_resend) {
            JSONObject params = new JSONObject();
            params.put("phone", phone);
            params.put("type", type);
            showProgress();
            loginAndRegisterModel.getCode(params);
        }
    }

    @Override
    public void onVerifyCodeSuccess(BaseBean baseBean) {
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
//                AppUtil.showToast(this, "验证成功");
                if (type == 1) {//注册
                    startActivity(getIntent().setClass(this, RegisterPwdActivity.class));
                } else if (type == 2) {//忘记密码
                    startActivity(getIntent().setClass(this, ResetPwdActivity.class));
                    finish();
                } else if (type == 3) {//验证码登录
                }
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "验证码不正确");
        }
    }

    @Override
    public void onVerifyCodeFailure() {
        AppUtil.showToast(this, "网络错误，请稍后再试！");
    }

    @Override
    public void onGetCodeSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                AppUtil.showToast(this, "验证码发送成功");
                vcvCode.resetValue();
                vcvCode.setText(null);
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "验证码发送失败，请重试");
        }
    }

    @Override
    public void onGetCodeFailure() {
        hideProgress();
        AppUtil.showToast(this, "网络错误，请稍后再试！");
    }

    @Override
    protected void onDestroy() {
        if (timer != null) {
            timer.cancel();
        }
        if (loginAndRegisterModel != null) {
            loginAndRegisterModel.removeListener();
        }
        super.onDestroy();
    }

}
